//
//  TFLMainViewController.m
//  netEasy
//
//  Created by mac on 15/10/31.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "TFLMainViewController.h"
#import "TFLNewsViewController.h"
#import "TFLReadingViewController.h"
#import "TFLAdViewController.h"
#import "TFLNavViewController.h"
#import "TFLLeftView.h"
#import "TFLRightMenuViewController.h"
//左边侧这栏的FRAME值
#define TFLleftViewW 150
#define TFLleftViewH 300
#define TFLleftViewY 50

//右边侧划栏的FRAME值
#define TFLRightViewW 260

//缩放用到的基本参数
#define TFLViewTransformY 50
#define TFLViewTransformX 150


@interface TFLMainViewController ()<TFLLeftViewDelegate>
@property(nonatomic, weak) UIViewController * showingController;
@property(nonatomic, weak) UIButton * maskBtn;
@property (weak, nonatomic) IBOutlet UIImageView *backgroudImageView;

@end

@implementation TFLMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //0.基础设置
    [self setupBasic];
    
    //1.创建所有的子控制器
    [self setupAllChildrend];
    
    //2.创建左边视图
    [self setupLeftView];
    
    
    //3.创建右边视图
    [self setupRightView];
    
    [self.view addSubview:self.childViewControllers[0].view];
    self.showingController = self.childViewControllers[0];
    
    NSLog(@"%@",NSStringFromCGRect(self.view.frame));
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/**
 * 主控制器的基本设置
 */
-(void)setupBasic
{
    //设置背景IMAGEVIEW的点击事件
    self.backgroudImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.backgroudImageView addGestureRecognizer:tap];

}
/**
 *  创建所有的子控制器
 */
-(void)setupAllChildrend
{

    //创建新闻控制器页面
    TFLNewsViewController * newCtr = [[TFLNewsViewController alloc] init];
    [self setCtr:newCtr title:@"新闻"];
    //创建阅读控制器页面
    TFLReadingViewController *readCtr = [[TFLReadingViewController alloc] init];
    [self setCtr:readCtr title:@"订阅"];
    //创建图片控制器页面
    UIViewController *photoCtr = [[UIViewController alloc] init];
    [self setCtr:photoCtr title:@"图片"];
    //创建广告控制器页面
    TFLAdViewController *adCtr = [[TFLAdViewController alloc] init];
    [self setCtr:adCtr title:@"视频"];
    //创建跟贴控制器页面
    UIViewController *commentCtr = [[UIViewController alloc] init];
    [self setCtr:commentCtr  title:@"跟贴"];
    //创建电台控制器页面
    UIViewController *radioCtr = [[UIViewController alloc] init];
    [self setCtr:radioCtr  title:@"电台"];
    
    
}
/**
 *  添加子控制器到MAIN控制器中
 *
 *  @param vc
 *  @param title
 */
-(void)setCtr:(UIViewController *) vc title:(NSString *) title
{
    //给控制器页面设置相应的随机颜色
    vc.view.backgroundColor=TFLRadomRGB;
    
    //设置对应控制器页面头部标题
    vc.navigationItem.title=title;
    
    //设置导行栏左边按钮
    UIImage * nomalLeftImage = [UIImage imageNamed:@"top_navigation_menuicon"];
    vc.navigationItem.leftBarButtonItem=[UIBarButtonItem barButtonItemWith:nomalLeftImage highlighted:nil action:@selector(leftNavBtnClick) target:self];
    
    //设置导秆栏右边的按钮
    UIImage * nomalRightImage = [UIImage imageNamed:@"top_navigation_infoicon"];
    vc.navigationItem.rightBarButtonItem=[UIBarButtonItem barButtonItemWith:nomalRightImage highlighted:nil action:@selector(rightNavBtnClick) target:self];
    
    //用导行控制器进行包装处理
    TFLNavViewController *nav=[[TFLNavViewController alloc] initWithRootViewController:vc];
    
    [self addChildViewController:nav];
}
/**
 *  设置左边测边栏
 */
-(void)setupLeftView
{
    TFLLeftView * leftView = [[TFLLeftView alloc] init];

    leftView.width = TFLleftViewW;
    leftView.height = TFLleftViewH;
    leftView.x = 0;
    leftView.y = TFLleftViewY;
    
    leftView.delegate = self;
    
    [self.view addSubview:leftView];
    
}
/**
 *  设置右边测边栏
 */
-(void)setupRightView
{
    TFLRightMenuViewController *rightMenuCTR = [[TFLRightMenuViewController alloc] init];
    
    CGFloat translateX = self.view.width - rightMenuCTR.view.width;
    rightMenuCTR.view.x=translateX;
    
    [self.view addSubview:rightMenuCTR.view];

}
/**
 *  点击导行栏左边按钮
 */
-(void)leftNavBtnClick
{

    NSLog(@"leftleft");
//    self.showingController.view.x=TFLleftViewW;
    
    
    //实现左边缩放效果
    [self infactTransformLeft];

}
/**
 *  点击导行栏右边按钮
 */
-(void)rightNavBtnClick
{
    
    NSLog(@"rightright");
    
    //实现右边缩放效果
    [self infactTransformRight];
    
    
}
/**
 *  实现左边缩放效果
 */
-(void)infactTransformLeft
{
    //计算出要缩放的比例
    CGFloat scale = (self.view.height - TFLViewTransformY*2)/self.view.height;
    
    //计算出要平移的X值
    CGFloat translateX = TFLleftViewW - self.view.width*(1-scale)*0.5;
    
    //计算出要平移的Y值
    CGFloat translateY = TFLleftViewY - self.view.height*(1-scale)*0.5;
    
    //动画实现相应的缩放效果
    [UIView animateWithDuration:1.0 animations:^{
        
        self.showingController.view.transform=CGAffineTransformMakeScale(scale, scale);
        
        CGAffineTransform rotationTransform = self.showingController.view.transform;
        
        self.showingController.view.transform = CGAffineTransformTranslate(rotationTransform, translateX/scale, translateY/scale);
        
    } completion:^(BOOL finished) {
        
        [self addCoverMaskToCtr];
        
    }];
}
/**
 *  实现右边缩放效果
 */
-(void)infactTransformRight
{
    
    //计算出要缩放的比例
    CGFloat scale = (self.view.height - TFLViewTransformY*2)/self.view.height;
    
    //计算出要平移的X值
    CGFloat translateX = TFLRightViewW - self.view.width*(1-scale)*0.5;
    //206
    
    //计算出要平移的Y值
    CGFloat translateY = TFLleftViewY - self.view.height*(1-scale)*0.5;
    
    //动画实现相应的缩放效果
    [UIView animateWithDuration:1.0 animations:^{
        
        self.showingController.view.transform=CGAffineTransformMakeScale(scale, scale);
        
        CGAffineTransform rotationTransform = self.showingController.view.transform;
        
        self.showingController.view.transform = CGAffineTransformTranslate(rotationTransform, -translateX/scale, translateY/scale);
        
    } completion:^(BOOL finished) {
        
        [self addCoverMaskToCtr];
        
    }];

}

/**
 *  添加遮罩
 */
-(void)addCoverMaskToCtr
{
    //定义按钮设置为遮罩
    UIButton * btnMask = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btnMask.frame=self.showingController.view.bounds;
    
    btnMask.backgroundColor = TFLColorWithRGBA(99, 154, 63, 0.5);
    
    [btnMask addTarget:self action:@selector(maskClick:) forControlEvents:UIControlEventTouchUpInside];
    //将遮罩放到当前显示的控制器上
    self.maskBtn = btnMask;
    [self.showingController.view addSubview:btnMask];
    

}
/**
 *  点击遮罩还原效果
 *
 *  @param btn
 */
-(void)maskClick:(UIButton *) btn
{
    
    
    [UIView animateWithDuration:1.0 animations:^{
        
        self.showingController.view.transform=CGAffineTransformIdentity;
        
        
    } completion:^(BOOL finished) {
        
        [btn removeFromSuperview];
        
    }];


}
/**
 *  背景图片的点击事件
 */
-(void)tapAction
{
    [self maskClick:self.maskBtn];

}
/**
 *  实现切换控制器页面的代理方法
 *
 *  @param leftView <#leftView description#>
 *  @param from     <#from description#>
 *  @param to       <#to description#>
 */
-(void)leftView:(TFLLeftView *)leftView from:(TFLLeftViewCTRType)from to:(TFLLeftViewCTRType)to
{
    //1.移除当前主控制器上的显示的控制器的VIEW
    [self.showingController.view removeFromSuperview];
    [self.maskBtn removeFromSuperview];
    
    //2.添加点击按钮对应的控制器的VIEW到主控制器上去
    UIViewController *currentCTR= self.childViewControllers[to];
    
    UIView * currentView = currentCTR.view;
    
    [self.view addSubview: currentView];
    
    //3.这里我们在设置当前要显示的VIEW的TRANSFORM值
    currentView.transform = self.showingController.view.transform;


    //4.这里附值当前显示的控制器
    self.showingController = currentCTR;
    
    //5.添加遮罩效果
    [self addCoverMaskToCtr];
    
    //6.将当前显示的控制器页面还原到正常
    [self maskClick:self.maskBtn];
}

@end
